/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.apiGenerator.dataType.entityInterface.resourceInfo;

import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.item.ClassGenerator;
import com.inspur.edp.caf.generator.item.ClassGeneratorContext;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.cef.designtime.api.IGspCommonDataType;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.cef.generator.apiGenerator.dataType.entityInterface.DataTypeInterfaceGeneratorContext;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;

public abstract class DataTypeResourceInfoGenerator extends ClassGenerator {

  protected final DataTypeInterfaceGeneratorContext parentContext;
  protected final IGspCommonDataType dataType;
  private String className;

  public DataTypeResourceInfoGenerator(
      DataTypeInterfaceGeneratorContext parentContext, IGspCommonDataType dataType) {
    super(parentContext.getApiGenContext().getCompilationUnitInfo());
    this.parentContext = parentContext;
    this.dataType = dataType;
    this.className = dataType.getCode() + "ResourceInfo";
  }

  @Override
  protected void beforeInitialize() {
    super.beforeInitialize();
    parentContext.getApiGenContext().getCefGenContext()
        .getEntityResourceInfos()
        .put(dataType.getCode(),
            new MediateType(parentContext.getApiGenContext().getPackageName(), getName()));
  }

  @Override
  protected ClassGeneratorContext createClassInfoContext() {
    return new ClassGeneratorContext();
  }

  @Override
  protected final ArrayList<Modifier.ModifierKeyword> getAccessModifier() {
    return new ArrayList<Modifier.ModifierKeyword>(
        Arrays.asList(Modifier.ModifierKeyword.PUBLIC_KEYWORD, ModifierKeyword.FINAL_KEYWORD));
  }

  @Override
  protected final TypeInfo setParentClass() {
    return getParentClass();
  }

  protected abstract TypeInfo getParentClass();

  @Override
  protected final String getName() {
    return className;
  }

  @Override
  protected final ArrayList<ClassMethodGenerator> createConstructorGenerators() {
    ArrayList<ClassMethodGenerator> methods = super.createConstructorGenerators();
    if (methods == null) {
      methods = new ArrayList<>();
    }
    methods.add(getCtorGenerator(className));
    return methods;
  }

  protected ClassMethodGenerator getCtorGenerator(String className) {
    return new ResourceInfoCtorGenerator(className, dataType);
  }

  @Override
  protected final ArrayList<ClassMethodGenerator> createMethodGenerators() {
    ArrayList<ClassMethodGenerator> methods = super.createMethodGenerators();
    if (methods == null) {
      methods = new ArrayList<>();
    }
    methods.add(getInitColumnsGenerator());
    createExtendMethodGenerators(methods);
    return methods;
  }

  protected void createExtendMethodGenerators(List<ClassMethodGenerator> methods){}

  protected InitColumnsGenerator getInitColumnsGenerator() {
    return new InitColumnsGenerator(dataType, parentContext);
  }
}
